<template>
  <div class="login-container">
    <div class="loginForm-container">
    <el-form class="login-form" autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left">
      <div class="title-container c-fc-fff">
        <div class="width290 margin-0auto">
          <p class="margin-r10 fl" >
            <!-- <svg-icon icon-class="logo" style="vertical-align: middle;" class="margin-l c-fs-28 c-mt-f3" /> -->
            <img src="../../assets/6o-logo.png" alt="" style="margin:5px -2px 0 5px">
          </p>
          <p class="fl">{{$t('login.title')}}</p>
        </div>

      </div>
      <div class="title">{{$t('login.subTitle')}}</div>
      <el-form-item class="input username" prop="username">
        <!--<span class="svg-container svg-container_login">-->
          <!--<svg-icon icon-class="user" />-->
        <!--</span>-->
        <el-input class="el-input" name="username" type="text" v-model="loginForm.username" autoComplete="on" placeholder="请输入用户名" />
      </el-form-item>

      <el-form-item class="input" prop="password">
        <!--<span class="svg-container">-->
          <!--<svg-icon icon-class="password" />-->
        <!--</span>-->
        <el-input class="el-input" name="password" :type="passwordType" v-model="loginForm.password" autoComplete="on" placeholder="请输入密码" />
        <span class="show-pwd" @click="showPwd">
          <svg-icon v-if="invisible" icon-class="eye" />
          <i v-else class="el-icon-view"></i>
        </span>

      </el-form-item>

      <el-form-item class="input" prop="captcha">
        <div class="captcha-container">
          <!--<span class="svg-container">-->
            <!--<svg-icon icon-class="form"/>-->
          <!--</span>-->
          <el-input class="el-input" name="captcha" type="text" v-model="loginForm.captcha" placeholder="请输入验证码" @keyup.enter.native="handleLogin" />
          <img v-show="captchaImg!=null" :src="captchaImg" @click="loadCaptcha" title="点击图片重新获取验证码" style="cursor: pointer;">
        </div>
      </el-form-item>
      <!--<div class="clearfix modifyPassword">-->
        <!--<el-checkbox class="fl" v-model="checked">记住密码</el-checkbox>-->
        <!--<span class="fr font-s12" @click="modifyPassword">忘记密码</span>-->
      <!--</div>-->
      <div class="login">
      <el-button class="loginBtn" :class="{active: loginForm.username&&loginForm.password&&loginForm.captcha}"type="primary" v-loading.fullscreen.lock="fullscreenLoading" @click.native.prevent="handleLogin">{{$t('login.logIn')}}</el-button>
      </div>
      <!--<div class="center font-s14">没有账号？<b class="register" @click="linkToRegister">立即注册</b></div>-->
    </el-form>
    <div class="info">XXXX版权所有 浙ICP备xxxxxxx号</div>
    </div>
  </div>
</template>

<script>
import { getCaptcha } from '@/api/login'

export default {
  name: 'login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value || value.trim() == '') {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!value || value.trim() == '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    const validateCaptcha = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入验证码'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: null,
        password: null,
        captcha: null,
        captchaKey: null
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        captcha: [{ required: true, trigger: 'blur', validator: validateCaptcha }]
      },
      passwordType: 'password',
      fullscreenLoading: false,
      captchaImg: null,
      invisible: true
    }
  },
  mounted() {
    this.loadCaptcha()
  },
  methods: {
    loadCaptcha() {
      getCaptcha().then(res => {
        this.captchaImg = res.data.data.img
        this.loginForm.captchaKey = res.data.data.key
      }).catch(err => {
        console.log(err)
      })
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
        this.invisible = false
      } else {
        this.passwordType = 'password'
        this.invisible = true
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.fullscreenLoading = true
          this.$store.dispatch('LoginByUsername', this.loginForm).then((res) => {
            this.$router.push({ path: '/' })
            this.fullscreenLoading = false
          }).catch(() => {
            this.loadCaptcha()
            this.fullscreenLoading = false
          })
        } else {
          this.loadCaptcha()
          console.log('error submit!!')
          return false
        }
      })
    },
    afterQRScan() {
      // const hash = window.location.hash.slice(1)
      // const hashObj = getQueryObject(hash)
      // const originUrl = window.location.origin
      // history.replaceState({}, '', originUrl)
      // const codeMap = {
      //   wechat: 'code',
      //   tencent: 'code'
      // }
      // const codeName = hashObj[codeMap[this.auth_type]]
      // if (!codeName) {
      //   alert('第三方登录失败')
      // } else {
      //   this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
      //     this.$router.push({ path: '/' })
      //   })
      // }
    },
    linkToRegister() {
      this.$router.push({ path: `/login/register` })
    }
  },
  created() {
    // window.addEventListener('hashchange', this.afterQRScan)
  },
  destroyed() {
    // window.removeEventListener('hashchange', this.afterQRScan)
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg:#2d3a4b;
$light_gray:#eee;

/* reset element-ui css */
.login-container {

  .el-input {
    display: inline-block;
    height: 42px;
    width: 300px;
    border: 0;
    border-bottom: 1px solid #ccc;
    input {
      height: 42px;
      width: 300px;
      border: 0;
      border-bottom: 1px solid #ccc;
      -webkit-appearance: none;
      border-radius: 0px;
      /*padding: 6px 5px 6px 0px;*/
      padding-left: 0;
      color: #353535;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px #fff inset !important;
        /*-webkit-text-fill-color: #fff !important;*/
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#4a9beb;
/*$dark_gray:#889aa4;*/
/*$light_gray:#eee;*/

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: #3b96e9;
  background-image: url(../../assets/login_bg.jpg);
  background-position: 50% 50%;
  background-repeat: no-repeat;
  background-size: auto 100%;
.login-form {
    position: relative;
    width: 400px;
    height: 370px;
    /*padding: 35px 35px 15px 35px;*/
    padding-left: 0;
    background-color: #fff;
    border-radius: 10px;
    .input {
      color: #353535;
      height: 30px;
      width: 300px;
      margin: 0px auto 30px;
      font-size: 14px;
      border: 0;
      border-bottom: 1px solid #ccc;
    }
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .title-container {
    position: absolute;
    width: 270px;
    height: 60px;
    line-height: 60px;
    text-align: center;
    left: 50%;
    top: -30px;
    margin-left: -135px;
    background-color: #529fee;
    border-radius: 10px;
    font-size: 18px;
    color: #fff;
    box-shadow: 0px 3px 3px rgba(82,159,238,.5);
    .set-language {
      color: #fff;
      position: absolute;
      top: 5px;
      right: 0px;
    }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: #fff;
    vertical-align: middle;
    width: 36px;
    display: inline-block;
  }
  }
.title {
  font-size: 16px;
  font-weight: 400;
  color: #529fee;
  margin: 50px auto 10px auto;
  text-align: center;
  padding-top: 50px;
}
.login{
  width: 300px;
  height: 44px;
  margin: 50px auto 40px;
  font-size: 14px;
  .loginBtn {
    display: inline-block;
    width: 300px;
    height: 44px;
    background-color: rgba(82,159,238,.3);
    border: 0;
  }
}
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: #889aa4;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 35px;
    bottom: 28px;
  }
  .captcha-container {
    /*display:flex;*/
    /*justify-content: space-between;*/
    position: relative;
    img {
      position: absolute;
      width: 120px;
      height: 38px;
      bottom: 6px;
      right: 0;
      text-align: center;
      line-height: 38px;
    }
  }
}
.info{
  width: 100%;
  text-align: center;
  margin: 30px auto 0;
  color: #fff;
  font-size: 12px;

}
  .login .loginBtn.active{
    background-color: #529fee;
  }
.login-form .input .el-input {
  padding: 0;
}
  .register {
    color: #529fee;
  }
.login-container .loginForm-container {
  width: 400px;
  position: absolute;
  left: 50%;
  top: 15%;
  margin-left: -200px;

  }
  .modifyPassword {
    width: 300px;
    margin: 24px auto;
  }
</style>
